﻿// -----------------------------------------------------------------------
// <copyright file="CertificateHalper.cs" company="None">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ODataRegression.Framework.Core.Shared
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Security.Cryptography.X509Certificates;
    using System.Net;
    using System.Diagnostics;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class CertificateHalper
    {
       public static  X509Certificate userCert = null;
       public static X509Certificate GetClientCertificate(string username)
        {
            if (userCert == null)
            {
                X509Store certStore = new X509Store(StoreLocation.CurrentUser);
                certStore.Open(OpenFlags.ReadOnly);
                X509Certificate2Collection certList = certStore.Certificates;


                foreach (X509Certificate cert in certList)
                {
                    if (cert.Subject == "CN=" + username)
                    {
                        userCert = cert;
                    }
                }

            }
           
            return userCert;

        }

        public static X509Certificate2Collection GetClientCertificates()
        {
                X509Store certStore = new X509Store(StoreLocation.CurrentUser);
                certStore.Open(OpenFlags.ReadOnly);
                return certStore.Certificates;
        }
    }
}
